<?php

namespace app\admin\controller\events;

use app\common\controller\Backend;
use fast\Http;
use think\Controller;
use think\Request;
/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Mailsys extends Backend
{
    
    /**
     * VMailsys模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('VMailsys');
        $this->view->assign("statusList", $this->model->getStatuslist());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    
     public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->count();   
            $list = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();    

            foreach($list as $value){ 
                if (!empty($value['attachment']))
                {
                    $this->get_annex($value);
                }
            }
        
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {   
                $params = $this->Setparams($params);
                foreach ($params as $k => &$v)
                {
                    $v = is_array($v) ? implode(',', $v) : $v;
                }
                try
                {
                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $this->model->validate($validate);
                    }
                    $result = $this->model->allowField(true)->save($params);
                    if ($result !== false)
                    {
                        
                       //获取到数据，进行把数据post发送到服务器
                       //发生变化通知服务器
                       //将字符串转换为时间戳
                       $params['starttime'] = strtotime($params['starttime']);
                       $params['endtime'] = strtotime($params['endtime']);
                       Http::get($this->GameDBConfig['api_url'].'mailsys',$params);
                       $this->success();
                      
                    }
                    else
                    {
                        
                        $this->error($this->model->getError());
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }

    //解析附件，分类出来,如果以后有新的类型也在此处加入就ok了。主要作用是用于界面显示使用。
    public function get_annex($list)
    {   
        $annex = json_decode($list['attachment']);

        for ($i=0; $i < count($annex); $i++) 
        { 
            if($annex[$i]->type == '1')
            {
                $list['diamond'] = $annex[$i]->value;
                $list['roomcard'] = $annex[$i]->value;
            }
            elseif ($annex[$i]->type == '2') {
                $list['gold'] = $annex[$i]->value;
            }
            elseif ($annex[$i]->type >= '100') {
                $list['giftcode'] = $annex[$i]->value;
            }
        }
    
    }

    //从新塑造add结构
    public function Setparams($value='')
    {
      //提取属性组装,附件
      $attachment = [];
      if (!empty($value['diamond'])) {
        $diamond = array('type' => 1,"value" => $value['diamond']);
        array_push($attachment,$diamond);
      }
      if (!empty($value['roomcard'])) {
        $diamond = array('type' => 1,"value" => $value['roomcard']);
        
        array_push($attachment,$diamond);
      }
      if (!empty($value['gold'])) {
        $diamond = array('type' => 2,"value" => $value['gold']);
        array_push($attachment,$diamond);
      }
      if (!empty($value['giftcode'])) {
        $diamond = array('type' => 100,"value" => $value['giftcode']);
        array_push($attachment,$diamond);
      }
        $reattachment = json_encode($attachment);
        $value['attachment'] = $reattachment;
        return $value;
    }

}
